package com.deyou.activity;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.deyou.R;
import com.deyou.base.BaseActivity;
import com.deyou.fragment.CalenderPopUpFragment;
import com.deyou.util.DateUtil;
import com.deyou.util.Logger;
import com.deyou.widget.DateRangeDecorator;
import com.squareup.timessquare.CalendarCellDecorator;
import com.squareup.timessquare.CalendarPickerView;
import com.squareup.timessquare.DefaultDayViewAdapter;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HouseCalenderSettingActivity extends BaseActivity {

    @BindView(R.id.tv_mid_title)
    TextView tvMidTitle;
    @BindView(R.id.calender_view)
    CalendarPickerView calenderView;
    @BindView(R.id.btn_setting_calender)
    TextView btnSettingCalender;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_calender_setting);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected boolean translucentStatusBar() {
        return super.translucentStatusBar();
    }

    private void initView() {
        tvMidTitle.setText("日历设置");

        Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR, 1);

        calenderView.setCustomDayView(new DefaultDayViewAdapter());
        calenderView.setDecorators(Arrays.<CalendarCellDecorator>asList(new DateRangeDecorator(HouseCalenderSettingActivity.this)));
        calenderView.init(new Date(), nextYear.getTime()) //
                .inMode(CalendarPickerView.SelectionMode.RANGE);

    }

    @OnClick({R.id.iv_left_back, R.id.btn_setting_calender})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_left_back:
                finish();
                break;
            case R.id.btn_setting_calender:
                List<Date> dateList = calenderView.getSelectedDates();
                StringBuilder sb = new StringBuilder();
                if (dateList.size() > 0 && dateList.size() < 2) {

                } else if (dateList.size() >= 2) {
                    for (Date date : dateList) {
                        sb.append(DateUtil.date2String10(date)).append("\n");
                    }
                } else {
                }
                showDialogFragment(sb.toString());
                break;
        }
    }

    public void showDialogFragment(String date) {
        FragmentTransaction mFragTransaction = getFragmentManager().beginTransaction();
        Fragment fragment = getFragmentManager().findFragmentByTag("dialogFragment");
        if (fragment != null) {
            //为了不重复显示dialog，在显示对话框之前移除正在显示的对话框
            mFragTransaction.remove(fragment);
        }
        CalenderPopUpFragment calenderPopUpFragment = CalenderPopUpFragment.newInstance(date);
        calenderPopUpFragment.show(mFragTransaction, "calenderPopUpFragment");
    }
}
